package com.beemans.common.ui.views.exposure

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout

/**
 * @author tiamosu
 * @date 2022/3/20
 *
 * 描述：定义一个Layout作为曝光组件的父布局
 */
open class ExposureLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    //定义曝光处理类
    protected val exposureHandler by lazy { ExposureHandler(this) }

    /**
     * 添加到视图
     */
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        exposureHandler.onAttachedToWindow()
    }

    /**
     * 从视图中移除
     */
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        exposureHandler.onDetachedFromWindow()
    }

    /**
     * 视图焦点改变
     */
    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        super.onWindowFocusChanged(hasWindowFocus)
        exposureHandler.onWindowFocusChanged(hasWindowFocus)
    }

    /**
     * 视图可见性
     */
    override fun onVisibilityAggregated(isVisible: Boolean) {
        super.onVisibilityAggregated(isVisible)
        exposureHandler.onVisibilityAggregated(isVisible)
    }

    /**
     * 曝光回调
     */
    fun setExposureCallback(callback: IExposureCallback) {
        exposureHandler.setExposureCallback(callback)
    }

    /**
     * 设置曝光条件 曝光区域大小，例如展示超过50%才算曝光
     */
    fun setShowRatio(ratio: Float) {
        exposureHandler.setShowRatio(ratio)
    }

    /**
     * 设置曝光最小时间，例如必须曝光超过两秒才算曝光
     */
    fun setTimeLimit(timeLimit: Long) {
        exposureHandler.setTimeLimit(timeLimit)
    }
}